<template>
  <div>
  <el-form
    :model="form"
    :rules="rules"
    ref="form"
    label-width="120px"
    class="user-form-width"
  >
    <el-form-item
      prop="email"
      :label="$t('settings.me.email')"
    >
      <el-input
        :placeholder="$t('settings.me.email')"
        v-model="form.email"
      >
      </el-input>
    </el-form-item>
    <el-form-item
      prop="name"
      :label="$t('settings.me.name')"
    >
      <el-input
        :placeholder="$t('settings.me.name')"
        v-model="form.name"
      ></el-input>
    </el-form-item>
    <el-form-item
      prop="password"
      :label="$t('settings.me.password')"
    >
      <el-input
        show-password
        :placeholder="$t('settings.me.password')"
        v-model="form.password"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <button
        type="button"
        role="primary"
        @click="submit()"
      >{{$t('common.save')}}</button>
      <button type="button" role="text" @click="openUpdatePasswordModal">修改密码</button>
    </el-form-item>
    </el-form>
    <update-password ref="updatePassword" :account="form.account"></update-password>
  </div>
</template>
<script>
import UpdatePassword from '@/components/settings/UpdatePassword'
import { updateUser, queryLoginUser, checkEmail } from '@/assets/api/index'
const BLUR = 'blur'
export default {
  data () {
    return {
      oldEmail: '',
      form: {},
      rules: {
        email: [
          { type: 'email', trigger: BLUR, message: this.$t('validationMessage.emailFormat') },
          { validator: this.validateEmail, trigger: BLUR }
        ],
        password: [
          { required: true, message: this.$t('validationMessage.inputRequired'), trigger: BLUR },
          { min: 6, max: 12, trigger: BLUR, message: this.$t('validationMessage.lengthRange', { min: 6, max: 12 }) }
        ],
        name: [
          { min: 3, max: 12, trigger: BLUR, message: this.$t('validationMessage.lengthRange', { min: 6, max: 12 }) }
        ]
      }
    }
  },
  components: { UpdatePassword },
  mounted () {
    this.getUser()
  },
  methods: {
    getUser () {
      queryLoginUser({}).then(res => {
        this.form = res.data
        this.oldEmail = res.data.email
      })
    },
    submit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.account = this.form.email
          updateUser(this.form).then(res => {
            if (res.data === 'failed') {
              this.$message({ type: 'warning', message: this.$t('common.operationFailed') })
            } else {
              this.$message({ type: 'success', message: this.$t('common.saved') })
            }
          })
        }
      })
    },
    validateEmail (rules, value, callback) {
      if (this.oldEmail === value) {
        callback()
      } else {
        checkEmail({ email: value }).then(res => {
          if (res.data) {
            callback()
          } else {
            callback(new Error(this.$t('register.emailAlreadyExist')))
          }
        })
      }
    },
    validateReenterPassword (rules, value, callback) {
      if (value === this.form.password) {
        callback()
      } else {
        callback(new Error(this.$t('settings.me.reenterPasswordValidationMessage')))
      }
    },
    validateOldPassword (rules, value, callback) {
      queryLoginUser({ password: value }).then(res => {
        if (res.data !== null && typeof res.data === 'object' && res.data.email === this.oldEmail) {
          callback()
        } else {
          callback(new Error(this.$t('settings.me.wrongPassword')))
        }
      })
    },
    openUpdatePasswordModal () {
      this.$refs.updatePassword.toggle()
    }
  }
}
</script>
